//
//  CustomTabBarViewController.m
//  CloudsOutpatient
//
//  Created by zzs on 16/1/29.
//  Copyright © 2016年 HC. All rights reserved.
//

#import "CustomTabBarViewController.h"
#import "CustomButtonItem.h"


@interface CustomTabBarViewController ()
{
    NSMutableArray *buttonArray;
    UIImageView *bgImgView;
}

@end

@implementation CustomTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    buttonArray=[[NSMutableArray alloc] init];
    // Do any additional setup after loading the view.
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
     //[self hideRealTabBar];
     [self customTabBar];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)hideRealTabBar{
    
     for(UIView *view in self.view.subviews){
        
        if([view isKindOfClass:[UITabBar class]]){
            
            view.hidden = YES;
            
            break;
            
            }
        
        }
    
}
- (void)customTabBar{
    if (!bgImgView){
        NSArray* normalImageArray = [NSArray arrayWithObjects:@"tab_hom_nor",@"tab_Cla_nor",@"tab_spe_nor",@"tab_bag_nor",@"tab_me_nor", nil];//item 正常状态下的背景图片
        NSArray* selectedImageArray = [NSArray arrayWithObjects:@"tab_hom_pre", @"tab_Cla_pre",@"tab_spe_pre",@"tab_bag_pre",@"tab_me_pre",nil];//item被选中时的图片名称
        NSArray *titleArray=[NSArray arrayWithObjects:@"春来庄",@"分类",@"",@"购物袋",@"我", nil];
        bgImgView= [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"tabbg2.png"]];
        bgImgView.backgroundColor=[UIColor colorWithHexString:@"fafafa"];
        bgImgView.frame = CGRectMake(0, -1,SCREEN_WIDTH,self.tabBar.frame.size.height+1 );
        [self.tabBar addSubview:bgImgView];
        UIView *line=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
        line.backgroundColor=[UIColor colorWithHexString:@"dcdcdc"];
        // [imgView addSubview:line];
        
        //创建按钮
        
        int viewCount = self.viewControllers.count > 5 ? 5 : self.viewControllers.count;
        
        self.buttons = [NSMutableArray arrayWithCapacity:viewCount];
        
        double _width = SCREEN_WIDTH / viewCount;
        
        double _height = self.tabBar.frame.size.height;
        
        for (int i = 0; i < viewCount; i++) {
            CustomButtonItem *btn = [[CustomButtonItem alloc] initWithFrame:CGRectMake(i*_width,0, _width, _height) norImage:normalImageArray[i] selectedImage:selectedImageArray[i] titile:titleArray[i]];
            if (i==2) {
                btn.isNormal=YES;
                btn.height=124/2;
                btn.bottom=_height;
            }
            [btn addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
            [buttonArray addObject:btn];
            btn.tag = i;
            
            [self.buttons addObject:btn];
            [self.tabBar addSubview:btn];
        }
        
        [self selectedTab:[self.buttons objectAtIndex:0]];
    }
  
    
}
- (void)selectedTab:(UIButton *)button{
    
    if (self.currentSelectedIndex == button.tag) {
        
    }
    for(UIButton *bt in buttonArray){
        if (bt==button) {
            bt.selected=YES;
        }else{
            bt.selected=NO;
        }
    }
    self.currentSelectedIndex = button.tag;
    
    self.selectedIndex = self.currentSelectedIndex;
    
  //   [self performSelector:@selector(slideTabBg:) withObject:button];
    
     }


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
